package com.carl;

import com.carl.mapper.DataSourceMapper;
import com.carl.mapper.StudentMapper;
import com.carl.model.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.Map;

public class TestMapperCache {

    private SqlSessionFactory factory;

    @Before
    public void setUp() throws Exception {
        String environment = "test";// demo test
        factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"),environment);
    }

    @Test
    public void student_getById() {

        SqlSession session = factory.openSession();//创建session实例
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        Student student = mapper.getStudentById(1);
        System.out.println(student.toString());
        Student student2 = mapper.getStudentByIdWithClassInfo(1);
        System.out.println(student2.toString());

        session.close();//关闭session
    }

    @Test
    public void dataSource_student(){
        SqlSession session = factory.openSession();

        DataSourceMapper mapper = session.getMapper(DataSourceMapper.class);

        List<Map> tableList = mapper.listTable();
        System.out.println(tableList.toString());

        List<Map> columns = mapper.listTableColumn("student");
        System.out.println(columns.toString());


        //关闭session
        session.close();
    }


}
